const EventEmitter = require('../../common/EventEmitter');
const config = require('../../common/config');
const requester = require('../../common/requester');
const $$onLoad = require('../../appLoaded/onLoad');
const commonReq = require('../../utils/commonReq');
const formIdInit = require('../../utils/formId.js');
const shareCodeHandler = require('../../utils/shareCodeHandler');
const attendanceHandler = require('../../utils/attendanceHandler');
const app = getApp();

Page({
    data: {
        isAuthed: true,
        pageLoaded: false
    },

    onLoad: function () {
        formIdInit({
            page: this
        });
        this.init();
    },

    onShow: function () {
        if (this.data.pageLoaded) {
            this.init();
        }
    },

    init: function () {
        Promise.all([
            this.queryPointRule(),
            this.checkAttendanceStatus()
        ]).then(res => {
            this.onPullDownStop();
        })
    },

    queryPointRule: function () {
        return requester.request({
            url: requester.service.queryPointRule
        }).then(res => {
            let {
                pointAccountVo,
                pointRuleVoList
            } = res.data;

            this.setData({
                pointAccountVo,
                pointRuleVoList
            })
        })
    },

    checkAttendanceStatus: function () {
        let hasAttendance = attendanceHandler.hasAttendanceToday();
        this.setData({
            hasAttendance
        })
    },

    goAttendance: function () {
        if (this.data.hasAttendance) {
            wx.showToast({
                title: '今天已经签过到了哦～',
                icon: 'none'
            });
            return
        }
        let businessSource = config.businessSource.attendance;
        //请求赚积分接口
        this.makePoint(businessSource)
            .then(attendanceHandler.updateAttendanceDate)
            .then(res => {
                this.setData({
                    hasAttendance: true
                })
            })
            .catch(e => {
                console.log(e)
            });
    },

    makePoint: function (businessSource) {
        return commonReq.makePoint(businessSource)
            .then(res => {
                wx.showToast({
                    icon: 'success',
                    title: res.data.makePointDesc,
                    duration: 2000
                })
            })
    },

    viewPointRecord: function () {
        wx.navigateTo({
            url: '../awardRecordList/awardRecordList'
        })
    },

    goGetAward: function (e) {
        let item = e.target.dataset.info;

        if (item.buttonJumpPage === 'shareFriend') {
            return;
        }

        if (item.buttonJumpPage.indexOf('housePublish') !== -1) {
            wx.switchTab({
                url: `/${item.buttonJumpPage}`
            });
            return
        }
        console.log(item);
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        this.init();
    },

    /***
     * 下拉刷新停止
     */
    onPullDownStop: function () {
        console.log("onPullDownStop.....");
        wx.stopPullDownRefresh();
    },

    onShareAppMessage: function (res) {
        return {
            ...shareCodeHandler.getShareDetail()
        };
    }
});
